package com.example.bmicalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //获取用户输入体重、身高
        EditText weight = findViewById(R.id.weight);
        String weight_temp = weight.getText().toString();
        double weight_num = Double.valueOf(weight_temp);
        EditText height = findViewById(R.id.height);
        String height_temp = height.getText().toString();
        double height_num = Double.valueOf(height_temp);

        DecimalFormat df = new DecimalFormat("######0.00");
        //计算BMI
        double res = (double)(weight_num/(height_num*height_num));
        String result = df.format(res);
        TextView resText = findViewById(R.id.BMI);
        resText.setText(result);

        //提出建议
        TextView advice = findViewById(R.id.advice);
        String advice_text;
        if(res<18.5)
            advice_text = "你的BMI指数较低，属于较瘦。" +
                    "高蛋白、高热量饮食绝对是健康增重者的首选。少食多餐、同补充消化酵素以增加对食物的消化吸收率亦都好重要。" +
                    "另外建议瘦弱的朋友，进行一些力量运动，如杠铃、哑铃等等。";
        else if((res>=18.5)&&(res<23.9))
            advice_text = "你的BMI指数正常，是最好的身体状态。建议你还是要通过适当的健身来继续保持身材";
        else if((res>=23.9)&&(res<27.9))
            advice_text = "你BMI指数超重了，稍微有点胖了。建议你少吃零食、甜膩、油炸类高热量食物，多吃蔬菜水果和膳食纤维" +
                    "及富含营养、低热量的食品，同时开始做有氧运动，提高代謝速率，加速消耗体内的脂肪和热量。";
        else
            advice_text = "你BMI指数已经超标了，已经达到肥胖一族的标准。建议不吃垃圾食品和过于油腻的食物，" +
                    "多吃富含营养但是低热量的食品，每天坚持1个小时以上的中等或高等强度运动。";
        advice.setText(advice_text);
    }
}